package com.ccl.iot.dialogs;import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.ccl.iot.template.ActivityDialog;
import com.ccl.iot.template.ActivityDialogTitled;
import com.ccl.iot.publics.Window;
import com.ccl.iot.IOTNetAPIConst;
import com.ccl.iot.R;

public class SecurityCodeDialog extends ActivityDialogTitled {
	public static final String KEXTRA_NAME_MOBILE = "Mobile";
	public static final String KEXTRA_NAME_EMAIL = "EMail";
	
	public interface SecurityCodeListener extends ActivityDialog.DialogInterface{
		public void onRegetSecurityCode(String aMobile, String aEMail);
		public boolean onConfirmSecurityCode(String aMobile, String aEMail, int aSecurityCode);
	}
	
	
	public static void Show(Context aContext, SecurityCodeListener aSecurityCodeListener, String aMobile, String aEMail){
		Bundle tBundle = ActivityDialog.ObtainBundle(false, false);
		
		tBundle.putString(KEXTRA_NAME_MOBILE, aMobile);
		tBundle.putString(KEXTRA_NAME_EMAIL, aEMail);
		
		ActivityDialog.Show(aContext, SecurityCodeDialog.class, tBundle, aSecurityCodeListener);
	}
	
	
	private SecurityCodeListener iSecurityCodeListener = null;
	private String iMobile = null;
	private String iEMail = null;
	
	private EditText iCode = null;
	
	private Button iReget = null;
	private Button iOK = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		super.setBackable(true);
		
		initialize();
		
		initializeLayout();
	}
	
	private void initialize(){
		iSecurityCodeListener = (SecurityCodeListener) super.getDialogInterface();
		
		iMobile = this.getIntent().getStringExtra(KEXTRA_NAME_MOBILE);
		iEMail = this.getIntent().getStringExtra(KEXTRA_NAME_EMAIL);
	}

	private void initializeLayout(){
		this.setTitle(R.string.Title_SecurityCodeDialog);
		this.setContentView(R.layout.securitycode_dialog);
		
		iCode = (EditText) this.findViewById(R.id.securitycode_code);
		
		iReget = (Button) this.findViewById(R.id.securitycode_reget);
		iOK = (Button) this.findViewById(R.id.securitycode_ok);
				
		handleInputEvent();
				
		handleButtonEvent();
	}
	
	private int getTextLength(TextView aText){
		if (aText != null){
			return aText.getText().length();
		}
		
		return 0;
	}
	
	private boolean isInfoValid(){
		return getTextLength(iCode) == IOTNetAPIConst.KIOT_SECURITY_CODE_LENGTH;
	}
	
	private void handleUsefulInfoChange(EditText aInfo){
		if (aInfo != null){
			aInfo.addTextChangedListener(new TextWatcher(){
				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
				}

				@Override
				public void afterTextChanged(Editable s) {
					iOK.setEnabled(isInfoValid());
				}		
			});
		}
	}
	
	private void handleInputEvent(){
		if (iCode != null){
			iCode.requestFocus();
			
			Window.ShowInputDialog(getContext());
			
			handleUsefulInfoChange(iCode);
			
			iCode.setOnEditorActionListener(new OnEditorActionListener(){
				@Override
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
					if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)){
						confirm();
					}
					
					return false;
				}
			});
		}
	}
	
	private void confirm(){
		if (isInfoValid()){
			if (iSecurityCodeListener != null){
				int tCode = Integer.valueOf(iCode.getText().toString());
				
				if (iSecurityCodeListener.onConfirmSecurityCode(iMobile, iEMail, tCode)){
					this.dismiss();
				}
			}
		}
	}
	
	private void handleButtonEvent(){
		if (iReget != null){
			iReget.setEnabled(false);
			
			iReget.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (iSecurityCodeListener != null){
						iSecurityCodeListener.onRegetSecurityCode(iMobile, iEMail);
					}
				}
			});
		}
		
		if (iOK != null){
			iOK.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v) {
					confirm();
				}
			});
		}
	}
}
